package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql="insert into shop (shopId,shopName,shopPhone,shopAddress,shopDescribe,addTime,shopProvince,shopCity,shopDistrict,shopLatitude,shopLongitude) values(null,?,?,?,?,?,?,?,?,?,?)";
        Object obj[]={
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                new Date(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopLatitude(),
                shop.getShopLongitude()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        JDBCDruidUtil.close();
        return num;
    }

    @Override
    public int delete(int shopId) {
        String sql="delete from shop where shopId=?";
        Object obj[]={shopId};
        int num = JDBCDruidUtil.update(sql, obj);
        JDBCDruidUtil.close();
        return num;
    }

    @Override
    public int update(Shop shop) {
        String sql="update shop set shopName=?,shopPhone=?,shopAddress=?,shopDescribe=?,shopProvince=?,shopCity=?,shopDistrict=?,shopLatitude=?,shopLongitude=?,shopStatus=? where shopId=?";
        Object obj[]={
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopStatus(),
                shop.getShopId()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        JDBCDruidUtil.close();
        return num;
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list=new ArrayList<>();
        //分页查询 模糊查询 时间范围查询
        String sql="select * from shop where 1=1 ";
        List params=new ArrayList();
        //判断如果map集合中有shopName 则该sql需要进行模糊查询
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){ //姓名部位null和空字符串
            sql+="and shopName like ?";
            params.add("%"+shopName+"%");
        }
        //判断是否需要进行手机号模糊查询
        String shopPhone = map.get("shopPhone");
        if (shopPhone!=null && !"".equals(shopPhone)){ //姓名部位null和空字符串
            sql+="and shopPhone like ?";
            params.add("%"+shopPhone+"%");
        }
        //判断是否需要进行日期范围查询
        String startTime=map.get("startTime");
        if (startTime!=null && !"".equals(startTime)){
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //排序
        sql+=" order by shopId desc ";
        //判断是否有分页操作
        String pn=map.get("pageNum");
        if (pn!=null && !"".equals(pn)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize= Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        //查询
        Object[] obj=params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while(true){
            try {
                if (!res.next()) break;
                Shop shop=new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getDate(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getInt(15)
                );
                list.add(shop);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return list;

    }

    @Override
    public int selectCount(Map<String, String> map) {

        int count=0;
        //分页查询 模糊查询 时间范围查询
        String sql="select count(1) from shop where 1=1 ";
        List params=new ArrayList();
        //判断如果map集合中有shopName 则该sql需要进行模糊查询
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){ //姓名部位null和空字符串
            sql+="and shopName like ?";
            params.add("%"+shopName+"%");
        }
        //判断是否需要进行手机号模糊查询
        String shopPhone = map.get("shopPhone");
        if (shopPhone!=null && !"".equals(shopPhone)){ //姓名部位null和空字符串
            sql+="and shopPhone like ?";
            params.add("%"+shopPhone+"%");
        }
        //判断是否需要进行日期范围查询
        String startTime=map.get("startTime");
        if (startTime!=null && !"".equals(startTime)){
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //查询
        Object[] obj=params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while(true){
            try {
                if (!res.next()) break;
                count=res.getInt(1);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        JDBCDruidUtil.close();
        return count;
    }
}
